<!doctype html>
<html>
  <head>
    <meta charset="utf-8" />
    <title></title>
  </head>
  <body>
    <!-- <img src="http://8.129.39.183:8080/uploadFile/girl.jpg" width="200px" height="200px"> -->
    用户名：<input type="text" id="name" /> 类型：<input type="text" id="userType" />
    客服code：<input type="text" id="kefuCode" /> 访客code：<input type="text" id="fangkeCode" />
    <button value="注册一个用户" onclick="zhuce()">注册一个用户</button>
    <br />
    收件人：<input type="text" id="shoujianren" /> 内容：<input type="text" id="content" />
    <button value="发送" onclick="fasong()">发送</button>
  </body>

  <script type="text/javascript" src="js/jquery-1.9.1.min.js"></script>
  <script type="text/javascript" src="js/jquery.actual.min.js"></script>
  <script type="text/javascript" src="js/bootstrap.min.js"></script>
  <script type="text/javascript" src="js/fileinput.min.js"></script>
  <script type="text/javascript" src="js/zh.js"></script>
  <script type="text/javascript" src="js/chatroom.js"></script>

  <script>
    var socket
    var userId = ''
    var userType = ''

    function zhuce(name) {
      var name = $('#name').val()
      //console.log(name);
      userId = name
      userType = $('#userType').val()
      socket = new WebSocket('ws://8.129.39.183:3333')
      console.log(socket)
      socket.onopen = (e) => {
        setTimeout(function (event) {
          console.log('WebSocket已成功连接！')
          console.log(socket)
          ws.register()
        }, 1000)
      }
      socket.onmessage = function (event) {
        var json = JSON.parse(event.data)
        console.log(json)
        if (json.status == 200) {
          var type = json.data.type
          console.log('收到一条新信息，类型为：' + type)
          switch (type) {
            case 'REGISTER':
              //ws.registerReceive();
              console.log(json.data)
              alert(JSON.stringify(json.data))
              break
            case 'SINGLE_SENDING':
              console.log('收到的消息是：' + JSON.stringify(json.data))
              alert(JSON.stringify(json.data))
              //ws.singleReceive(json.data);

              break
            case 'GROUP_SENDING':
              ws.groupReceive(json.data)
              break
            case 'FILE_MSG_SINGLE_SENDING':
              ws.fileMsgSingleRecieve(json.data)
              break
            case 'FILE_MSG_GROUP_SENDING':
              ws.fileMsgGroupRecieve(json.data)
              break
            default:
              console.log('不正确的类型！')
          }
        } else {
          alert(json.msg)
          console.log(json.msg)
        }
      }
    }

    function fasong() {
      var fromUserId = userId
      var toUserId = $('#shoujianren').val()
      var news = $('#content').val()
      var fangkeCode = $('#fangkeCode').val()
      var kefuCode = $('#kefuCode').val()
      ws.singleSend(fromUserId, toUserId, news, kefuCode, fangkeCode)
    }

    //  var userId="009";
    //     var socket;
    //     var sentMessageMap;
    // console.log("aaaa");

    // if(!window.WebSocket){
    //         window.WebSocket = window.MozWebSocket;
    //     }
    //     if(window.WebSocket){
    //         //socket = new WebSocket("ws://8.129.39.183:3333");
    //         socket = new WebSocket("ws://127.0.0.1:3333");

    //         // 连接成功1秒后，将用户信息注册到服务器在线用户表
    //         socket.onopen = setTimeout(function(event){
    //               console.log("WebSocket已成功连接！");
    //               ws.register();
    //         }, 1000)

    //         socket.onclose = function(event){
    //               console.log("WebSocket已关闭...");
    //         };
    //     } else {
    //           alert("您的浏览器不支持WebSocket！");
    //     }
  </script>
</html>
